<?php
if (!ob_start("ob_gzhandler")) {
  ob_start();
}
require_once dirname(__FILE__) . '/incls/php/config.php';
require_once dirname(__FILE__) . '/incls/php/miscutils.class.php';
require_once dirname(__FILE__) . '/incls/php/dbutils.class.php';
session_name(SESSION);
session_start();
$myPdo = DbUtils::createPdoInst();

$sys_script = basename(__FILE__, '.php');

$cond_vals = new stdClass();
$cond_vals->c = 't.script = :v1';
$cond_vals->v = array(':v1' => $sys_script);
$opt = new stdClass();
$opt->select_expr = 't.*, t1.folder AS _folder';
$opt->join_expr = 'LEFT JOIN pz2_ui AS t1 ON t1.id = t.ui_id';
$tmp = DbUtils::get($myPdo, 'pz2_sys', $cond_vals, NULL, NULL, NULL, NULL, $opt)->d;
if (count($tmp) == 0) {
  die('No system found');
}

$system = $tmp[0];

$page_id = MiscUtils::getParam('i', NULL);
$logout = MiscUtils::getParam('logout', NULL);
$url = MiscUtils::getParam('url', NULL);
if ($logout) {
  if (isset($_SESSION[$sys_script . USER])) {
    unset($_SESSION[$sys_script . USER]);
  }
  header('location: ' . preg_replace('/&logout=1/', '', MiscUtils::curPageURL()));
  return;
}
if ($system->is_login_required == 1 && $page_id != $system->login_page_id && !isset($_SESSION[$sys_script . USER])) {
  $url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  header('location: http://' . URL . $sys_script . '.php?i=' . $system->login_page_id . '&url=' . urlencode($url));
  return;
}


$cond_vals = new stdClass();
$cond_vals->c = 't.sys_id = :v1';
$cond_vals->v = array(':v1' => $system->id);
$opts = new stdClass();
$opts->select_expr = 't.id, t.parent_id, t.title, t.order, t.is_visible, t.is_default, t.path';
$pages = DbUtils::get($myPdo, 'pz2_page', $cond_vals, 't.order', 'ASC', NULL, NULL, $opts)->d;
if (!$page_id) {
  foreach ($pages as &$page) {
    if ($page->is_default == 1) {
      $page_id = $page->id;
    }
  }
}
unset($page);
if (!$page_id) {
  die('No homepage');
}

$cond_vals = new stdClass();
$cond_vals->c = 't.id = :v1';
$cond_vals->v = array(':v1' => $page_id);
$page = DbUtils::get($myPdo, 'pz2_page', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d[0];

$cond_vals = new stdClass();
$cond_vals->c = 't.page_id = :v1';
$cond_vals->v = array(':v1' => $page_id);
$opt = new stdClass();
$opt->select_expr = 't.*, t1.folder AS _folder';
$opt->join_expr = 'LEFT JOIN pz2_plugin AS t1 ON t1.id = t.plugin_id';
$instances = DbUtils::get($myPdo, 'pz2_plugin_instance', $cond_vals, NULL, NULL, NULL, NULL, $opt)->d;
foreach ($instances as &$instance) {
  $instance_script = _script($instance->_folder, $instance->id, $instance->width, $instance->height);
  $page->html = str_replace('{' . $instance->_folder . ':' . $instance->id . '}', $instance_script, $page->html);
}
include dirname(__FILE__) . "/exts/uis/$system->_folder/index.php";
return;

function _script($folder, $instance_id, $width, $height) {
  $script_name = MiscUtils::getParam('script', 'index');
  return "<div id=\"plgn_dev_$instance_id\" style=\"padding: 0;margin: 0;border: 0;width: $width" . "px; height: $height" . "px\">
            <script type=\"text/javascript\">
              function updateHeight_$instance_id() {
                var ifm_$instance_id = document.getElementById(\"plgn_ifm_$instance_id\");
                var dev_$instance_id = document.getElementById(\"plgn_dev_$instance_id\");
                if (ifm_$instance_id && !window.opera){
                  if (ifm_$instance_id.contentDocument && ifm_$instance_id.contentDocument.body){
                    var body = ifm_$instance_id.contentDocument.body;
                    var html = ifm_$instance_id.contentDocument.documentElement;
                    ifm_$instance_id.height = Math.max(body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight, $height);
                    dev_$instance_id.style.height = Math.max(body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight, $height) + 'px';
                  } else if (document.all('plgn_ifm_$instance_id') && plgn_ifm_$instance_id.document.body) {
                    document.all('plgn_dev_$instance_id').style.height = plgn_ifm_$instance_id.document.body.scrollHeight + 'px';
                    document.all('plgn_ifm_$instance_id').style.height = plgn_ifm_$instance_id.document.body.scrollHeight + 'px';
                    var my_footer = document.getElementById(\"my_footer\");
                    if (my_footer && my_footer.offsetTop < (parseFloat(plgn_ifm_$instance_id.document.body.scrollHeight, 10) + 150)) {
                      my_footer.style.position = 'absolute'; 
                      my_footer.style.top = parseFloat(plgn_ifm_$instance_id.document.body.scrollHeight, 10) + 150;
                      my_footer.style.left = 0;
                    }
                  }
                }
              }
            </script>
            <iframe id=\"plgn_ifm_$instance_id\" name=\"plgn_ifm_$instance_id\" onload=\"Javascript:updateHeight_$instance_id()\" width=\"$width\" height=\"$height\" scrolling=\"no\" frameborder=\"0\"  marginwidth=\"0\" marginheight=\"0\" allowtransparency=\"true\" src=\"exts/plugins/$folder/$script_name.php?inid=$instance_id&infold=$folder&" . $_SERVER['QUERY_STRING'] . "\"></iframe>
          </div>";
}
?>